package com.briansteen.sketches;

import java.awt.Desktop;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import com.briansteen.utils.GeoPosition;

import nanoxml.XMLElement;

import java.io.*;
import java.net.*;
import java.util.*;


public class FlickrApiTest {

	private String _api_key = "db31e74674556a3bed55e758302c1a23";
	private ArrayList<GeoPosition> positions = new ArrayList<GeoPosition>();
	private int photosAmount = 50;
	
	public FlickrApiTest() {
		try {
			System.out.println("start search");
			// get photo search xml
	        URLConnection uc = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + _api_key + "&per_page=" + photosAmount + "&has_geo=&extras=geo&woe_id=12596900&place_id=JRLFlY2YA5nA1sQh7g").openConnection();
	        DataInputStream dis = new DataInputStream(uc.getInputStream());
	        String nextline;
	        // go through each line
	        int i=0;
	        while ((nextline = dis.readLine()) != null) {
	            if(nextline.indexOf("photo id") != -1) {
	            	XMLElement xml = new XMLElement();
	                xml.parseString(nextline);
	                Enumeration enumm = xml.enumerateAttributeNames();
	                GeoPosition position = new GeoPosition(0,0);
	                while (enumm.hasMoreElements()) {
	                    String key = (String) enumm.nextElement();
	                    String value = xml.getStringAttribute(key);
	                    // extract latitude and logitude
	                    if(key.equals("LATITUDE"))
	                    	position.latitude = Float.parseFloat(value);
	                    if(key.equals("LONGITUDE"))
	                    	position.longitude = Float.parseFloat(value);
	                }
	                positions.add(position);
	            }
	        }
	        dis.close();
	        System.out.println("search finished");
		} catch (MalformedURLException me) {
            System.out.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.out.println("IOException: " + ioe);
        }
	}
	/*
	private GeoPosition getGeoLocation(String photo_id) {
		GeoPosition position = new GeoPosition(0,0);
		try {
			// get photo search
	        String flickrUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=" + _api_key + "&photo_id=" + photo_id;
			URLConnection uc = new URL(flickrUrl).openConnection();
	        DataInputStream dis = new DataInputStream(uc.getInputStream());
	        String nextline;
	        // go through each line
	        while ((nextline = dis.readLine()) != null) {
	            if(nextline.indexOf("<location") != -1) {
	            	nextline = nextline.substring(0, nextline.length()-1);
	            	nextline += " />";
	            	XMLElement xml = new XMLElement();
	                xml.parseString(nextline);
	                Enumeration enumm = xml.enumerateAttributeNames();
	                float id = 0;
	                while (enumm.hasMoreElements()) {
	                    String key = (String) enumm.nextElement();
	                    String value = xml.getStringAttribute(key);
	                    if(key.equals("LONGITUDE")) position.longitude = Float.parseFloat(value);
	                    if(key.equals("LATITUDE")) position.latitude = Float.parseFloat(value);
	                }
	            }
	        }
	        dis.close();
		} catch (MalformedURLException me) {
            System.out.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.out.println("IOException: " + ioe);
        }
        System.out.println(position.latitude + " " + position.longitude);
        return position;
    }
    */
    public static void main(String args[]) throws Exception {
    	FlickrApiTest main = new FlickrApiTest();
    }
}
